package com.ts.api.module.api.service.impl;

import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.valid.Assert;
import com.ts.api.common.constant.api.ApiBizTypeEnum;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.entity.bo.ApiBizMatchBO;
import com.ts.api.module.api.entity.bo.ApiKeyAmountBO;
import com.ts.api.module.api.entity.bo.ApiParameterBO;
import com.ts.api.module.api.entity.bo.ApiPreRequiredBO;
import com.ts.api.module.api.entity.bo.ApiRequiredFlowBO;
import com.ts.api.module.api.entity.bo.ApiUserRegisterBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.api.entity.vo.ReqApiParameterVO;
import com.ts.api.module.api.service.ApiBizDataExchangeService;
import com.ts.api.module.api.service.ApiBizRequiredService;
import com.ts.api.module.api.service.ApiKeyAmountService;
import com.ts.api.module.api.service.ApiMsgV1Service;
import com.ts.api.module.api.service.ApiUserRegister;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.product.entity.bo.ProductAmountBO;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.service.ProductAmountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class ApiMsgV1ServiceImpl implements ApiMsgV1Service {

    @Autowired
    private ApiBizRequiredService apiBizRequiredService;
    @Autowired
    private ApiBizDataExchangeService apiBizDataExchangeService;
    @Autowired
    private ProductAmountService productAmountService;
    @Autowired
    private ApiKeyAmountService apiKeyAmountService;
    @Autowired
    private ApiUserRegister apiUserRegister;

    @Override
    public void dataExchange(ApiKeyPO apiKeyPO, ReqApiParameterVO reqApiParameterVO) throws Exception {
        //  1.撞库前置校验-创建正常计划类型匹配（机构、平台）
        ApiBizMatchBO normalPlan = ApiBizMatchBO.createNormalPlan(true);
        ApiPreRequiredBO apiPreRequiredBO = ApiPreRequiredBO.create(reqApiParameterVO, ApiBizTypeEnum.DATA_EXCHANGE, null, normalPlan);
        ApiParameterBO apiParameterBO = apiBizRequiredService.preRequiredParam(apiPreRequiredBO);

        //  2.撞库匹配
        apiBizDataExchangeService.dataExchange(apiParameterBO);
    }

    @Override
    public void apply(ApiKeyPO apiKeyPO, ReqApiParameterVO reqApiParameterVO) {
        ApiUserRequest apiUserRequest = reqApiParameterVO.getApiUserRequest();

        //  1.查询是否渠道存在该流程
        String phone = apiUserRequest.getPhone();
        Assert.isNotEmptyThrows(phone, new ApiMsgException(ApiEnum.API_REQUIRED_PARAM_NULL.format("phone")));
        String phoneMd5 = MD5.create().digestToHex(phone);

        ApiRequiredFlowBO apiRequiredFlowBO = ApiRequiredFlowBO.createByApiClientId(phoneMd5, apiKeyPO.getApiClientId());
        ProductApplyFlowPO productApplyFlowPO = apiBizRequiredService.requiredSucDataExchangeFlow(apiRequiredFlowBO);
        Assert.isNotNullThrows(productApplyFlowPO, new ApiMsgException(ApiEnum.API_APPLY_FLOW_ERROR));

        //  2.进件前置参数验证
        ApiBizMatchBO normalPlan = ApiBizMatchBO.createNormalPlanApply();
        ApiPreRequiredBO apiPreRequiredBO = ApiPreRequiredBO.create(reqApiParameterVO, ApiBizTypeEnum.APPLY, null, normalPlan);
        ApiParameterBO apiParameterBO = apiBizRequiredService.preRequiredParam(apiPreRequiredBO);

        //  3.进件注册
        ApiUserRegisterBO apiUserRegisterBO = apiUserRegister.applyUserRegister(apiParameterBO, productApplyFlowPO);
        ChannelUserRegisterPO userRegisterPO = apiUserRegisterBO.getUserRegisterPO();

        //  4.下游计费
        ProductAmountBO productAmountBO = new ProductAmountBO();
        productAmountBO.setProductId(productApplyFlowPO.getLoanProductId());
        productAmountBO.setProductPlanId(productApplyFlowPO.getLoanProductPlanId());
        productAmountBO.setUserId(userRegisterPO.getId());
        productAmountBO.setPhoneMd5(phoneMd5);
        productAmountBO.setLoanChannelId(productApplyFlowPO.getLoanChannelId());
        productAmountBO.setApiClientId(productApplyFlowPO.getApiClientId());
        productAmountBO.setOriginalPrice(null);
        productAmountBO.setHasPlatform(Boolean.FALSE);
        productAmountBO.setHasLocalExist(apiParameterBO.getHasLocalExist());
        BigDecimal planPrice = productAmountService.deduct(productAmountBO);

        //  5.上游计费
        ApiKeyAmountBO apiKeyAmountBO = new ApiKeyAmountBO();
        apiKeyAmountBO.setProductId(productApplyFlowPO.getLoanProductId());
        apiKeyAmountBO.setProductPlanId(productApplyFlowPO.getLoanProductPlanId());
        apiKeyAmountBO.setUserId(userRegisterPO.getId());
        apiKeyAmountBO.setLoanChannelId(productApplyFlowPO.getLoanChannelId());
        apiKeyAmountBO.setApiClientId(productApplyFlowPO.getApiClientId());
        apiKeyAmountBO.setPhoneMd5(phoneMd5);
        apiKeyAmountBO.setPlanPrice(planPrice);
        apiKeyAmountBO.setHasPlatform(Boolean.FALSE);
        apiKeyAmountBO.setHasLocalExist(apiParameterBO.getHasLocalExist());
        apiKeyAmountService.deduct(apiKeyAmountBO);
    }
}
